package com.orientation.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.orientation.commom.Result;
import com.orientation.dto.ReserveGoodsDto;
import com.orientation.entity.Receive;
import com.orientation.service.ReceiveService;
import com.orientation.vo.ReceiveGoodsStudentVo;
import com.orientation.vo.StudentClassInfoVo;
import com.orientation.vo.StudentProgressVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * <p>
 * 物质领取状态表 前端控制器
 * </p>
 *
 * @author xuandian
 * @since 2022-01-09
 */
@Api(tags = "物资申领管理")
@RestController
@RequestMapping("/receive")
public class ReceiveController {
    @Resource
    private ReceiveService receiveService;

    @ApiOperation("添加物资申领")
    @PostMapping("/insertReceives")
    public ResponseEntity<Result> insertReceive(@RequestBody ReserveGoodsDto reserveGoodsDto) {
        receiveService.insertReceive(reserveGoodsDto);
        return ResponseEntity.ok(new Result());
    }

    @ApiOperation("获取已经申领/确领取物资")
    @GetMapping("/queryReceiveGoodsStudentVo")
    public ResponseEntity<Result> queryReceiveGoodsStudentVo(@RequestParam(required = false, defaultValue = "1") Integer pageNum,
                                                             @RequestParam(required = false, defaultValue = "10") Integer pageSize,
                                                             @RequestParam(required = false, value = "receiveStatus") Integer receiveStatus,
                                                             @RequestParam(value = "collegeId", required = false) String collegeId,
                                                             @RequestParam(value = "majorId", required = false) String majorId,
                                                             @RequestParam(value = "className", required = false) String className,
                                                             @RequestParam(value = "studentName", required = false) String studentName,
                                                             @RequestParam(value = "studentId", required = false) String studentId) {
        IPage<ReceiveGoodsStudentVo> receiveGoodsStudentVoIPage = receiveService.queryReceiveGoodsStudentVo(pageNum, pageSize, receiveStatus, collegeId, majorId, className, studentName, studentId);
        return ResponseEntity.ok(Result.success(receiveGoodsStudentVoIPage));
    }

    @ApiOperation("删除")
    @DeleteMapping("/delete/{id}")
    public ResponseEntity<Result> removeById(@PathVariable String id) {
        receiveService.removeById(id);
        return ResponseEntity.ok(new Result());
    }

    @ApiOperation("修改//新增")
    @PostMapping("/updateOrinsertReceive")
    public ResponseEntity<Result> updateOrinsertReceive(@RequestParam(value = "files", required = false) MultipartFile[] files,
                                                        @RequestParam(value = "sid") String sid,
                                                        @RequestParam(value = "urls", required = false) List<String> urls) {
        receiveService.updateOrinsertReceive(files, sid, urls);
        return ResponseEntity.ok(new Result());
    }

    @ApiOperation("查询单条")
    @GetMapping("/{id}")
    public Receive findById(@PathVariable String id) {
        return receiveService.getById(id);
    }

    @ApiOperation("物资申领列表导出")
    @GetMapping("/statisticsExport")
    public void statisticsExport(@RequestParam(value = "receiveStatus", required = false) Integer receiveStatus,
                                 @RequestParam(value = "majorId", required = false) String majorId,
                                 @RequestParam(value = "className", required = false) String className,
                                 @RequestParam(value = "studentName", required = false) String studentName,
                                 @RequestParam(value = "studentId", required = false) String studentId,
                                 HttpServletResponse response) {
        receiveService.statisticsExport(receiveStatus, majorId, className, studentName, studentId, response);
    }

    @ApiOperation("统计物资数完成进度")
    @GetMapping("/progressReceive")
    public ResponseEntity<Result> progressReceive(@RequestParam(required = false, defaultValue = "") String collegeId) {
        StudentProgressVo progress = receiveService.progressReceive(collegeId);
        return ResponseEntity.ok(Result.success(progress));
    }

    @ApiOperation("物资未领取查询")
    @GetMapping("/queryStudentClassInfo")
    public ResponseEntity<Result> queryStudentGoods(@RequestParam(required = false, defaultValue = "1") Integer pageNum,
                                                        @RequestParam(required = false, defaultValue = "10") Integer pageSize,
                                                        @RequestParam(value = "collegeId", required = false) String collegeId,
                                                        @RequestParam(value = "majorId", required = false) String majorId,
                                                        @RequestParam(value = "className", required = false) String className,
                                                        @RequestParam(value = "studentName", required = false) String studentName,
                                                        @RequestParam(value = "studentId", required = false) String studentId) {
        IPage<StudentClassInfoVo> studentClassInfoVoIPage = receiveService.queryStudentGoods(pageNum, pageSize, collegeId, majorId, className, studentName, studentId);
        return ResponseEntity.ok(Result.success(studentClassInfoVoIPage));
    }
}

